const express = require('express')
const app = express()
const path = require('path')
const multer = require("multer")
app.use(express.static(path.join(__dirname, '/public')))
const http = require('http').Server(app)
http.listen(4000)
const io = require('socket.io')(http)
const db = require('../../config/db')
app.use(express.static(__dirname + "./image"));
var fs = require('fs')
    // 自定义 multer 的 diskStorage 的存储目录与文件名

// io.on('connection') -- 固定的, 用于 监测有没有人用socket服务链接我, 触发后面的函数
io.on('connection', function(socket) { // socket连接者对象

    socket.on('message', data => { // 

        let time = new Date().getFullYear() + '-' + (new Date().getMonth() + 1) + '-' + new Date().getDate() + '-' + new Date().getHours() + ':' + new Date().getMinutes() + ':' + new Date().getSeconds()
        let { f_id, u_id, val, inof, uname, type, img, mediaRecorder, location, id, formData, duration } = data
        if (type == 0) {
            db.query('insert into chat(f_id,u_id,chat_val,chat_io,chat_to,chat_name,chat_email,chat_img,chat_time) value(?,?,?,?,?,?,?,?,?)', [f_id, u_id, val, 0, 1, inof.user_name, inof.user_email, inof.user_img, String(time)])
            const chat = { type, title: 'ws', name: inof.user_name || uname, msg: val, time, f_id: u_id, img: inof.user_img, id: u_id }
            io.sockets.emit(`index${f_id}`, { type: 0, name: inof.user_name || uname, lei: val, img: inof.user_img, time, id: u_id || id, chat })
            io.sockets.emit(f_id, chat) // 把当前收到的聊天消息, 发送给所连接着(前端)

        } else if (type == 1) {
            // 图片
            console.log(f_id);
            db.query('insert into chat(f_id,u_id,chat_val,chat_io,chat_to,chat_name,chat_email,chat_img,chat_time) value(?,?,?,?,?,?,?,?,?)', [f_id, u_id, img, 0, 1, inof.user_name, inof.user_email, img, String(time)])
            const chat = { type, title: 'ws', name: inof.user_name || uname, msg: img, time, f_id: u_id, img: inof.user_img, id: u_id }
            io.sockets.emit(`index${f_id}`, { type: 0, name: inof.user_name || uname, lei: '图片', img: inof.user_img, time, id: u_id, chat })
            io.sockets.emit(f_id, chat) // 把当前收到的聊天消息, 发送给所连接着(前端)
                // 定位
        } else if (type == 2) {
            console.log(data);
            const chat = { type, title: 'ws', name: inof.user_name || uname, res: location, msg: '', time, f_id: u_id, img: inof.user_img, id: u_id }
            io.sockets.emit(`index${f_id}`, { type: 0, name: inof.user_name || uname, lei: '对方发送了定位', img: inof.user_img, time, id: u_id, chat })
            io.sockets.emit(f_id, chat) // 把当前收到的聊天消息, 发送给所连接着(前端)
                // 语音
        } else if (type == 3) {
            const arraybuffer = new Int8Array(formData);
            // const blob = new Blob([arraybuffer], { type: 'application/pdf' })
            console.log(arraybuffer);

            const chat = { type, title: 'ws', name: inof.user_name || uname, msg: '语音', formData: arraybuffer, time, duration, f_id: u_id, img: inof.user_img, id: u_id }
                // const chat = { type, title: 'ws', name: inof.user_name || uname, msg: '语音', time, f_id: u_id, formData }
            io.sockets.emit(`index${f_id}`, { type: 0, name: inof.user_name || uname, lei: '语音', img: inof.user_img, time, to: 1, id: u_id, chat })
            io.sockets.emit(f_id, chat) // 把当前收到的聊天消息, 发送给所连接着(前端)
                // 视频
        } else if (type == '请求连接') {
            const chat = { type, title: 'ws', name: inof.user_name || uname, msg: mediaRecorder, time, f_id: u_id }
                // io.sockets.emit(`index${f_id}`, { type: 0, name: inof.user_name || uname, lei: val, img: inof.user_img, time, to: 1, id: u_id, chat })
            io.sockets.emit(f_id, chat) // 把当前收到的聊天消息, 发送给所连接着(前端)
                // 视频
        } else if (type == '同意') {
            console.log(data);
            const chat = { type, title: 'ws', name: inof.user_name || uname, msg: mediaRecorder, time, f_id: u_id }
                // io.sockets.emit(`index${f_id}`, { type: 0, name: inof.user_name || uname, lei: val, img: inof.user_img, time, to: 1, id: u_id, chat })
            io.sockets.emit(f_id, chat) // 把当前收到的聊天消息, 发送给所连接着(前端)

        } else {
            const chat = { type, title: 'ws', name: inof.user_name || uname, msg: '定位', time, f_id: u_id }
            io.sockets.emit(`index${f_id}`, { type: 0, name: inof.user_name || uname, lei: val, img: inof.user_img, time, to: 1, id: u_id, chat })
            io.sockets.emit(f_id, chat) // 把当前收到的聊天消息, 发送给所连接着(前端)
        }


        // db.query('SELECT * FROM ionum', (err, ionumres) => {
        //     if (err) return res.send({ code: 500, msg: err.sqlMessage })
        //     const iosNumfind = ionumres.find(item => item.f_id == f_id && item.u_id == u_id && item.ios > 0)
        //     if (iosNumfind) {
        //         iosNumfind.ios++
        //             db.query(`update ionum set ios = '${iosNumfind.ios}' where io_id =${iosNumfind.io_id}`, (err, res) => {
        //                 if (err) return res.send({ code: 500, msg: err.sqlMessage })

        //             })
        //     } else {
        //         db.query('insert into ionum(f_id,u_id,ios) value(?,?,?)', [f_id, u_id, 1], (err, resa) => {
        //             if (err) return res.send({ code: 500, msg: err.sqlMessage })
        //             const chat = { name: 'ws', msg: val, time }
        //             io.sockets.emit(`index${f_id}`, { type: 0, title: inof.user_name, lei: val, img: inof.user_img, time, to: ios, id: u_id, inof, chat })
        //             io.sockets.emit(f_id, chat) // 把当前收到的聊天消息, 发送给所连接着(前端)
        //         })


        //     }

        // })

    })

    // 好友申请


})




module.exports = io